// pages/my_address/my_address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList:'',
    store_id:0,
    status:1,
    sweep:1,
    room_id:0,    //房间id
    mac_code:'',  //设备 编码
    lock_code:'', //智能锁编码
    clean_log_id:0,
    user_name:'',
    par:'',
    power_off:1, //电源开关
    end:0, //智能锁临时密码到期时间

    power_off_array:[],
    showStatus:false,   //待打扫
    showSweep:false,   //打扫中
    isChecked1:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.data.store_id = e.store_id;
    this.getRoom(e)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getRoom:function (e) {
    var store_id = e.store_id;
    app.ajax({
      url: '/api/user/StaffEntranceRoomList',
      data: {
        store_id:store_id
      },
      success: res => {
        if(res.code == 1){
          console.log(res.data)
          this.setData({
            roomList:res.data,
            // end:this.data.end

          })
        }
      }
    })
  },
  /**
   * 取消
   */
  close:function(){
    this.setData({
      showSweep:false,
      showStatus:false,
    });
  },
  /**
   * 待打扫
   */
  showStatus:function(e) {
    this.data.room_id = e.currentTarget.dataset.id;
    this.data.mac_code = e.currentTarget.dataset.mac_code;
    this.data.lock_code = e.currentTarget.dataset.lock_code;
    this.data.power_off = e.currentTarget.dataset.power_off;
    this.data.end = e.currentTarget.dataset.end;
    var have = false;
    for (var i = 0; i < this.data.power_off_array.length; i++) {
      if(this.data.power_off_array[i].room_id == this.data.room_id){
        this.data.power_off = this.data.power_off_array[i].power_off;
        have = true;
      }
    }
    if(have == false){
      this.data.power_off_array.push({room_id:this.data.room_id,power_off:this.data.power_off,});
    }
    console.log(this.data.power_off_array);
    console.log('待打扫')
    console.log(this.data.room_id)
    console.log(this.data.mac_code)
    console.log(e.currentTarget.dataset)

    if(this.data.power_off == 1){
      this.data.isChecked1 = false;
    }else{
      this.data.isChecked1 = true;
    }

    this.setData({
      showStatus:true,
      isChecked1:this.data.isChecked1
    });
  },
  /**
   * 打扫中
   */
  showSweep:function (e) {
    this.data.room_id = e.currentTarget.dataset.id;
    this.data.mac_code = e.currentTarget.dataset.mac_code;
    this.data.lock_code = e.currentTarget.dataset.lock_code;
    this.data.clean_log_id = e.currentTarget.dataset.clean_log_id;
    this.data.power_off = e.currentTarget.dataset.power_off;
    this.data.user_name = e.currentTarget.dataset.user_name;
    var time = e.currentTarget.dataset.time;


    var have = false;
    for (var i = 0; i < this.data.power_off_array.length; i++) {
      if(this.data.power_off_array[i].room_id == this.data.room_id){
        this.data.power_off = this.data.power_off_array[i].power_off;
        have = true;
      }
    }
    if(have == false){
      this.data.power_off_array.push({room_id:this.data.room_id,power_off:this.data.power_off,});
    }


    console.log('打扫中')
    console.log(this.data.room_id)
    console.log(this.data.mac_code)
    console.log(this.data.lock_code)
    console.log(this.data.clean_log_id)
    console.log(this.data.user_name)

    if(this.data.power_off == 1){
      this.data.isChecked1 = false;
    }else{
      this.data.isChecked1 = true;
    }

    this.setData({
      showSweep:true,
      user_name:this.data.user_name,
      clean_id:this.data.clean_log_id,
      time:time,
      isChecked1:this.data.isChecked1
    });
  },
  /**
   * 开始打扫
   */
  startSweep:function (e) {
    app.ajax({
      url: '/api/user/StaffEntranceClean',
      data: {
        room_id:this.data.room_id
      },
      success: res => {
        if(res.code == 1){

          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 1000
          })
          setTimeout(function () {
            // wx.redirectTo({
            //   url: '/pages/store/store?store_id='+this.data.store_id
            // })
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      }
    })
  },

  // 获取房间密码
  getRoomPass:function(){
    if(this.data.lock_code =="" || this.data.lock_code==null){
      wx.showToast({
        title: "参数错误1",
        icon: 'none'
      })
      return
    }
    console.log(this.data);
    if(this.data.end =="" || this.data.end==null){
      wx.showToast({
        title: "参数错误2",
        icon: 'none'
      })
      return
    }
    app.ajax({
      url: '/api/user/getTemporaryPassword',
      data: {
        lock_code: this.data.lock_code,
        use_end_time: this.data.end,
        mac_code:this.data.mac_code
      },
      success: res => {
        if(res.code == 1){
          if(res.data.data.length === 0){
            wx.showToast({
              title:res.data.msg,
              icon: 'none'
            });return
          }
          this.setData({
            room_password: res.data.data.password,
           });
          wx.setStorageSync('roompassword_orderid_'+res.data.id, res.data.data.password);
          this.checkRoomPass()
        }else{
          wx.showToast({
            title: '获取失败',
            icon: 'none'
          })
        }
      }
    }, 1);
  },


    //点击复制密码
    checkRoomPass: function () {
      var that = this;
      wx.showModal({
        title: '房间密码',
        content: that.data.room_password,
        showCancel: true,
        confirmText: '复制密码',
        success(res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: that.data.room_password,
              success(res) {
                wx.getClipboardData({
                  success(res) {
                    console.log(res.data) // data
                    wx.showToast({
                      title: "复制成功",
                      icon: 'none'
                    })
                  },fail(res){
                    wx.showToast({
                      title: "复制失败",
                      icon: 'none'
                    })
                  }
                })
              }
            })
          }
        }
      })
    },

  /**
   * 结束打扫
   */
  endSweep:function (e) {
    app.ajax({
      url: '/api/user/StaffEntranceCleanEnd',
      data: {
        clean_id:this.data.clean_log_id
      },
      success: res => {
        if(res.code == 1){
          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 1000
          })
          setTimeout(function () {
            // wx.redirectTo({
            //   url: '/pages/store/store?store_id='+this.data.store_id
            // })
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      }
    })
  },
  changeSwitch1:function(e) {
    console.log(this.data)

    if(this.data.isChecked1){
      app.ajax({
        url: '/api/user/roomPowerOff',
        data: {
          mac_code:this.data.mac_code
        },
        success: res => {
          if(res.code == 1){
            for (var i = 0; i < this.data.power_off_array.length; i++) {
              if(this.data.power_off_array[i].room_id == this.data.room_id){
                this.data.power_off_array[i].power_off = 1;
              }
            }
            wx.showToast({
              title: res.msg,
              icon: 'success',
              duration: 1000
            })
          }
        }
      })
    }else{
      app.ajax({
        url: '/api/user/roomPowerOn',
        data: {
          mac_code:this.data.mac_code
        },
        success: res => {
          if(res.code == 1){
            for (var i = 0; i < this.data.power_off_array.length; i++) {
              if(this.data.power_off_array[i].room_id == this.data.room_id){
                this.data.power_off_array[i].power_off = 2;
              }
            }
            wx.showToast({
              title: res.msg,
              icon: 'success',
              duration: 1000
            })
          }
        }
      })
    }

    this.setData({
      isChecked1:!this.data.isChecked1
    });

  }
})